const daoCommon = require('../util/daoCommon');
const commonUtils = require('../util/commonUtils');
const sqlHelper = require('../util/sqlHelper');

const dao = {
    handleAddCase: (data) => {
        let sql;
        const id = commonUtils.genUUID();
        const date = new Date();
        const createTime = date.toLocaleString();
        data.id = id;
        data.createTime = createTime;
        sql = `INSERT INTO cases VALUES ($id, $name, $type, $recommend, $time,$url,$mainImg,$introduce,$createTime,null,null,null,'0','1')`;
        return daoCommon.queryCommon(sql, data);
    },
    handleGetCase: (data) => {
        let sql;
        sql = "select * from cases where is_delete='0'";
        return daoCommon.selectCommon(sql);
    },
    handleSearchCase: (data) => {
        let sql;
        if (data) {
            sql = "select * from cases where 1=1";
            if (data.name) {
                sql += " and case_name like concat('%',$name,'%')";
            }
            if (data.type) {
                sql += " and case_type=$type";
            }
            sql += " and is_delete='0'";
        } else {
            sql = "select * from cases where is_delete='0'";
        }
        return daoCommon.selectCommon(sql, data);
    },
    handleDeleteCase: (data) => {
        let is_relate = data.is_relate;
        if (is_relate == 1) {
            let sql = [];
            let sql1 = `update cases set is_delete='1' where case_id=$case_id`;
            let sql2 = `UPDATE advertisement SET is_delete='1' where advertisement_target=$case_id`;
            sql.push(sqlHelper.getNewSql(sql1, data));
            sql.push(sqlHelper.getNewSql(sql2, data));
            return sqlHelper.execTrans(sql);
        } else if (is_relate == 0) {
            let sql = "update cases set is_delete='1' where case_id=$case_id";
            return daoCommon.queryCommon(sql, data);
        }
    },
    handleStartCase: (data) => {
        let is_relate = data.is_relate;
        if (is_relate == 1) {
            let sql = [];
            let sql1 = `UPDATE cases SET is_enabled='1' where case_id=$case_id`;
            let sql2 = `UPDATE advertisement SET is_delete='0' where advertisement_target=$case_id`;
            sql.push(sqlHelper.getNewSql(sql1, data));
            sql.push(sqlHelper.getNewSql(sql2, data));
            return sqlHelper.execTrans(sql);
        } else if (is_relate == 0) {
            let sql;
            sql = "update cases set is_enabled='1' where case_id=$case_id";
            return daoCommon.queryCommon(sql, data);
        }
    },
    handleOutageCase: (data) => {
        let is_relate = data.is_relate;
        if (is_relate == 1) {
            let sql = [];
            let sql1 = `UPDATE cases SET is_enabled='0' where case_id=$case_id`;
            let sql2 = `UPDATE advertisement SET is_delete='2' where advertisement_target=$case_id`;
            sql.push(sqlHelper.getNewSql(sql1, data));
            sql.push(sqlHelper.getNewSql(sql2, data));
            return sqlHelper.execTrans(sql);
        } else if (is_relate == 0) {
            let sql;
            sql = "update cases set is_enabled='0' where case_id=$case_id";
            return daoCommon.queryCommon(sql, data);
        }
    },
    handleEditCase: (data) => {
        let sql;
        const date = new Date();
        const updated_time = date.toLocaleString();
        data.updated_time = updated_time;
        sql = "update cases set case_name=$name,case_type=$type,is_recommend=$recommend,achievement_time=$time";
        sql += ",case_url=$url,case_mainImg=$mainImg,case_introduce=$introduce,updated_time=$updated_time where case_id=$id";
        return daoCommon.queryCommon(sql, data);
    }
};
module.exports = dao;